package com.lwbldy.modules.student.controller;

import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.PageUtils;
import com.lwbldy.common.tools.StringUtils;

import com.lwbldy.modules.student.domain.SqlQuery;
import com.lwbldy.modules.student.service.SqlQueryService;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 查询配置 Controller
 * @author lwb
 * @email lwbldy@163.com
 * @date 2022-06-22 09:56:13
 */
@Controller
@Api(tags = "查询配置")
@RequestMapping("/student/sqlquery")
public class SqlQueryController {

    /**
     * Describe: 基础路径
     * */
    private static String MODULE_PATH = "student/sqlquery/";


    @Autowired
    private SqlQueryService sqlQueryService;

    @ApiOperation(value="获取列表视图")
    @GetMapping("/main")
    public String main(){
        return MODULE_PATH + "main";
    }


    @ApiOperation(value = "分页查询查询配置")
    @PostMapping("/list")
    @ResponseBody
    public R list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "15") Integer limit){
        PageHelper.startPage(page, limit);
        Map<String,Object> primaryMap = new HashMap<>();
        List<SqlQuery> brandList = sqlQueryService.queryList(primaryMap);
        return PageUtils.restPage(brandList);
    }


    @ApiOperation(value = "获取新增视图")
    @GetMapping("add")
    public String add(ModelMap model){
        return MODULE_PATH + "add";
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @ResponseBody
    public R save(@RequestBody SqlQuery sqlQuery){
        return R.resultRow(sqlQueryService.save(sqlQuery));
    }


    @ApiOperation(value = "获取修改视图")
    @GetMapping("edit")
    public String edit(ModelMap model, Integer id){
        model.put("sqlQuery",sqlQueryService.queryObject(id));
        return MODULE_PATH + "edit";
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("update")
    @ResponseBody
    public R update(@RequestBody SqlQuery sqlQuery){
        return R.resultRow(sqlQueryService.update(sqlQuery));
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("remove/{id}")
    @ResponseBody
    public R remove(@PathVariable Integer id){
        int result = sqlQueryService.delete(id);
        return R.resultRow(result);
    }

    @DeleteMapping("removeBatch/{ids}")
    @ApiOperation(value="批量删除数据")
    @ResponseBody
    public R removeBatch(@PathVariable String ids){
        int result = sqlQueryService.deleteBatch(StringUtils.StringIdsToArr(ids));
        return R.resultRow(result);
    }


    @ApiOperation(value="获取列表视图")
    @GetMapping("/user_main/{id}")
    public String user_main(@PathVariable Integer id,ModelMap modelMap){
        modelMap.put("sqlQuery",sqlQueryService.queryObject(id));
        return MODULE_PATH + "user_main";
    }

    @ApiOperation(value="获取列表视图")
    @GetMapping("/user_list/{id}")
    public String user_list(@PathVariable Integer id,ModelMap modelMap){
        modelMap.put("sqlQuery",sqlQueryService.queryObject(id));
        return MODULE_PATH + "user_main";
    }

    @ApiOperation(value = "分页查询查询配置")
    @PostMapping("/user_list/{id}")
    @ResponseBody
    public R list(@PathVariable Integer id){
        SqlQuery sqlQuery = sqlQueryService.queryObject(id);
        R r = R.ok();
        List list = sqlQueryService.queryBySql(sqlQuery.getSqlVar());
        r.put("count",list.size());
        r.put("data",list);
        return r;
    }

}
